package com.lemonPiggy.message.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.lemonPiggy.message.domain.MessageAnnouncementSend;
import com.lemonPiggy.message.model.AnnouncementSendModel;
import com.ruoyi.common.core.domain.R;
import com.lemonPiggy.message.service.IMessageAnnouncementSendService;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户通告阅读标记Controller
 * 
 * @author ruoyi
 * @date 2021-08-17
 */
@RestController
@RequestMapping("/send")
public class MessageAnnouncementSendController extends BaseController
{
    @Autowired
    private IMessageAnnouncementSendService messageAnnouncementSendService;

    /**
     * 查询用户通告阅读标记列表
     */
    @PreAuthorize(hasPermi = "system:send:list")
    @GetMapping("/list")
    public TableDataInfo list(MessageAnnouncementSend messageAnnouncementSend)
    {
        startPage();
        List<MessageAnnouncementSend> list = messageAnnouncementSendService.selectMessageAnnouncementSendList(messageAnnouncementSend);
        return getDataTable(list);
    }

    /**
     * 导出用户通告阅读标记列表
     */
    @PreAuthorize(hasPermi = "system:send:export")
    @Log(title = "用户通告阅读标记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageAnnouncementSend messageAnnouncementSend) throws IOException
    {
        List<MessageAnnouncementSend> list = messageAnnouncementSendService.selectMessageAnnouncementSendList(messageAnnouncementSend);
        ExcelUtil<MessageAnnouncementSend> util = new ExcelUtil<MessageAnnouncementSend>(MessageAnnouncementSend.class);
        util.exportExcel(response, list, "用户通告阅读标记数据");
    }

    /**
     * 获取用户通告阅读标记详细信息
     */
    @PreAuthorize(hasPermi = "system:send:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(messageAnnouncementSendService.selectMessageAnnouncementSendById(id));
    }

    /**
     * 新增用户通告阅读标记
     */
    @PreAuthorize(hasPermi = "system:send:add")
    @Log(title = "用户通告阅读标记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessageAnnouncementSend messageAnnouncementSend)
    {
        return toAjax(messageAnnouncementSendService.insertMessageAnnouncementSend(messageAnnouncementSend));
    }

    /**
     * 修改用户通告阅读标记
     */
    @PreAuthorize(hasPermi = "system:send:edit")
    @Log(title = "用户通告阅读标记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MessageAnnouncementSend messageAnnouncementSend)
    {
        return toAjax(messageAnnouncementSendService.updateMessageAnnouncementSend(messageAnnouncementSend));
    }

    /**
     * 删除用户通告阅读标记
     */
    @PreAuthorize(hasPermi = "system:send:remove")
    @Log(title = "用户通告阅读标记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(messageAnnouncementSendService.deleteMessageAnnouncementSendByIds(ids));
    }


       /**
     * 通过id查询
    * @param id
    * @return
    */
   @GetMapping(value = "/queryById/{id}")
   public AjaxResult  queryById(@PathVariable(name="id") String id) {
       return AjaxResult.success(messageAnnouncementSendService.queryByUserId(id));
   }

       /**
    * 获取我的消息
    * @return
    */
   @GetMapping(value = "/getMyAnnouncementSend")
   public R getMyAnnouncementSend(AnnouncementSendModel announcementSendModel) {
       startPage();
       announcementSendModel.setUserId(announcementSendModel.getUserId());
       List<AnnouncementSendModel> myAnnouncementSendPage = messageAnnouncementSendService.getMyAnnouncementSendPage(announcementSendModel);
       getDataTable(myAnnouncementSendPage);
       return R.ok(myAnnouncementSendPage);
   }

    /**
     * 更新用户系统消息阅读状态
     * @param anntId 通知id
     * @return
     */
    @PutMapping(value = "/editByAnntIdAndUserId/{anntId}")
    public R<MessageAnnouncementSend> editById(@PathVariable("anntId") String anntId) {
            MessageAnnouncementSend messageAnnouncementSend = new MessageAnnouncementSend();
            messageAnnouncementSend.setReadFlag("1");
            messageAnnouncementSend.setReadTime(new Date());
            messageAnnouncementSendService.updateMessageSend(messageAnnouncementSend,anntId);
        return R.ok();
    }

    /**
     * 一键已读
     * @return
     */
    @PutMapping(value = "readAll")
    public R<MessageAnnouncementSend> readAll(){
            MessageAnnouncementSend messageAnnouncementSend = new MessageAnnouncementSend();
            messageAnnouncementSend.setReadTime(new Date());
            messageAnnouncementSend.setReadFlag("1");
            messageAnnouncementSendService.updateRead(messageAnnouncementSend, SecurityUtils.getUserId());
        return R.ok();
    }
}
